package dao;

import java.util.ArrayList;

import org.hibernate.Session;


import metier.Offre;


public class DAOOffreHib extends DAOHibernate implements DAOOffre {

	@Override
	public Offre get(int id) throws Exception {
		Offre	offre = null;
		Session	session = connect();		
		offre = (Offre) session.get(Offre.class, id);	
		close(session);
		return offre;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Offre> loadAll() throws Exception {
		ArrayList<Offre>	tab = null;
		Session session = connect();
		
		tab = (ArrayList<Offre>) session.createQuery("FROM Offre")
				.list();
		
		close(session);
		return tab;
	}

	@Override
	public void save(Offre offre) throws Exception {
		Session	session = connect();
		
		session.save(offre);
		
		close(session);	
		
	}

	@Override
	public void saveOrupdate(Offre offre) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Offre offre) throws Exception {
		Session	session = connect();
		
		session.delete(offre);
		offre.setId_offre(-1);
		
		close(session);
		
	}

	@Override
	public void update(Offre offre) throws Exception {
		Session	session = connect();
		
		session.update(offre);
		
		close(session);
		
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Offre> search(String titre) throws Exception {
		ArrayList<Offre>	tab = null;
		Session session = connect();
		
		tab = (ArrayList<Offre>) session.createQuery("FROM Offre where titre LIKE '" +titre+"'")
				.list();
		
		close(session);
		return tab;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Offre> searchCategorie(int id_categorie) throws Exception {
		ArrayList<Offre>	tab = null;
		Session session = connect();
		//select o from Offre o join o.categorie o
		//tab = (ArrayList<Offre>) session.createQuery("FROM Offre  where id_categorie LIKE '" +id_categorie+"'")
		//tab = (ArrayList<Offre>) session.createQuery("select o from Offre o join o.categorie o where o.id_categorie LIKE '" +id_categorie+"'")
		//tab = (ArrayList<Offre>) session.createQuery("select o from Categorie c join c.offre o where o.id_offre LIKE '" +id_categorie+"'")
		tab = (ArrayList<Offre>) session.createQuery("select o from Offre o join o.categorie oc where oc.id_categorie LIKE '" +id_categorie+"'")
				.list();
		
		close(session);
		return tab;
	}

	//Select * from offre o, categorie c, offre_lier_categorie of where c.id_categorie = of.id_categorie and c.id_categorie = 1
	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Offre> searchRegion(int id_region) throws Exception {
		ArrayList<Offre>	tab = null;
		Session session = connect();
		 
	
		tab = (ArrayList<Offre>) session.createQuery("FROM Offre where id_region LIKE '" +id_region+"'")
				.list();
		
		close(session);
		return tab;
	}

	@Override
	public Offre detailsOffre(int id_offre) throws Exception {
		Offre	offre = null;
		Session	session = connect();		
		offre = (Offre) session.get(Offre.class, id_offre);	
		close(session);
		return offre;
	}
	

	
	
	
	
}
